package com.obtk.controllerSGW;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.obtk.modelSGW.ProProduct;
import com.obtk.modelSGW.ProStorage;
import com.obtk.service.ProProduceService;
import com.obtk.service.ProStorageService;
import com.obtk.util.CoreConst;
import com.obtk.util.PageUtil;
import com.obtk.util.ResultUtil;
import com.obtk.vo.base.PageResultVo;
import com.obtk.vo.base.ResponseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Arrays;
import java.util.List;

/**
 * 功能说明
 *
 * @author Mr.Sun
 * @date 2023-03-30 19:13:13
 */
@Controller
@RequestMapping("/product")
public class ProProductController {
    @Autowired
    private ProProduceService proProduceService;

    @PostMapping("/list")
    @ResponseBody
    public PageResultVo loadUsers(ProProduct proProduct, Integer limit, Integer offset, Model model) {
        System.out.println(proProduct);
        PageHelper.startPage(PageUtil.getPageNo(limit, offset), limit);
        List<ProProduct> proProductsList = proProduceService.selectProProduct(proProduct);
        PageInfo<ProProduct> pages = new PageInfo<>(proProductsList);
        System.out.println(proProductsList);
        return ResultUtil.table(proProductsList, pages.getTotal());
    }

    /**
     * 新增产品
     */
    @PostMapping("/add")
    @ResponseBody
    public ResponseVo add(ProProduct proProduct) {
        return proProduceService.register(proProduct);
    }


    /**
     * 编辑用户
     */
    @PostMapping("/edit")
    @ResponseBody
    public ResponseVo editUser(ProProduct proProductForm) {
        return proProduceService.updateByProProductId(proProductForm);
    }
    /**
     * 删除用户
     */
    @GetMapping("/delete")
    @ResponseBody
    public ResponseVo deleteUser(String prodId) {
        List<String> proProduceIdsList = Arrays.asList(prodId);
        int a = proProduceService.updateStatusBatch(proProduceIdsList, CoreConst.STATUS_INVALID);
        if (a > 0) {
            return ResultUtil.success("删除用户成功");
        } else {
            return ResultUtil.error("删除用户失败");
        }
    }


    @GetMapping("/batch/delete")
    @ResponseBody
    public ResponseVo batchDeleteUser(String proProductIdStr) {
        List<String> proProduceIdsList = Arrays.asList(proProductIdStr.split(","));
        int a = proProduceService.updateStatusBatch(proProduceIdsList, CoreConst.STATUS_INVALID);
        if (a > 0) {
            return ResultUtil.success("删除用户成功");
        } else {
            return ResultUtil.error("删除用户失败");
        }
    }

    @PostMapping("/lists")
    @ResponseBody
    public ResponseVo loadUser(ProProduct proProduct) {
        List<ProProduct> proProductsList = proProduceService.selectProProduct1(proProduct);
        System.out.println(proProductsList);
        return ResultUtil.success("查询成功",proProductsList);
    }

}
